package com.ssuu.ssuusnow.project.manage.dict_type.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ssuu.ssuusnow.project.manage.dict_type.entity.DictType;
import com.ssuu.ssuusnow.project.manage.dict_type.mapper.DictTypeMapper;
import com.ssuu.ssuusnow.project.manage.dict_type.service.IDictTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssuu.ssuusnow.project.manage.user.entity.User;
import com.ssuu.ssuusnow.utils.security.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 * 字典类型 服务实现类
 * </p>
 *
 * @author sun
 * @since 2023-06-14
 */
@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, DictType> implements IDictTypeService {

    @Resource
    DictTypeMapper dictTypeMapper;

    @Override
    public Object listTo(DictType t) {
        QueryWrapper<DictType> wrappers = new QueryWrapper<>();
        wrappers.orderByDesc("sort");
        wrappers.and(wrapper -> {
            wrapper.eq("isdel", false);
            wrapper.like("dictname", Objects.isNull(t.getDictname()) ? "" : t.getDictname());
//        wrapper.eq("status",Objects.isNull(t.getStatus())?"":t.getStatus());
        });
        Page<DictType> page = new Page<>(t.getCurrentPage(),t.getPageSize());
        Object userPage = t.getPageSize() > 0 && t.getCurrentPage() > 0 ? dictTypeMapper.selectPage(page, wrappers) : dictTypeMapper.selectList(wrappers);
        return userPage;
    }

    @Override
    public int AddTo(DictType t) {
        t.setCreateTime(LocalDateTime.now());
        t.setUpdateTime(LocalDateTime.now());
        User lou = SecurityUtils.LoginUsers();
        t.setUpdateby(lou.getId());
        return dictTypeMapper.insert(t);
    }

    @Override
    public int UpdateTo(DictType t) {
        t.setUpdateTime(LocalDateTime.now());
        User lou = SecurityUtils.LoginUsers();
        t.setUpdateby(lou.getId());
        return dictTypeMapper.updateById(t);
    }

    @Override
    public int DelTo(Long t) {
        DictType u = new DictType();
        u.setUpdateTime(LocalDateTime.now());
        u.setId(t);
        u.setIsdel(true);
        User lou = SecurityUtils.LoginUsers();
        u.setUpdateby(lou.getId());
        return dictTypeMapper.updateById(u);
    }
}
